package com.lrkj.web.controller.ureport;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lrkj.common.annotation.Log;
import com.lrkj.common.annotation.RepeatSubmit;
import com.lrkj.common.core.controller.BaseController;
import com.lrkj.common.core.domain.AjaxResult;
import com.lrkj.common.core.page.TableDataInfo;
import com.lrkj.common.enums.BusinessType;
import com.lrkj.common.utils.CommonUtils;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.poi.ExcelUtil;
import com.lrkj.system.service.ISysConfigService;
import com.lrkj.ureport.DTO.BaseTempExportDTO;
import com.lrkj.ureport.domain.BaseExportTemp;
import com.lrkj.ureport.service.IBaseExportTempService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ureport2模板管理Controller
 *
 * @author ruoyi
 * @date 2022-01-21
 */
@Api(value = "ureport2模板管理", tags = "ureport2模板管理")
@ApiSupport(author = "lrkj", order = 10)
@RestController
@RequestMapping("/baseExportTemp")
public class BaseExportTempController extends BaseController {
    @Autowired
    private IBaseExportTempService baseExportTempService;

    @Autowired
    Environment environment;

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询ureport2模板管理列表
     */
    @ApiOperation(value = "查询ureport2模板管理列表", notes = "查询ureport2模板管理列表")
    @ApiOperationSupport(order = 10)
    @PreAuthorize("@ss.hasPermi('base:baseExportTemp:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseExportTemp baseExportTemp) {
        startPage();
        List<BaseExportTemp> list = baseExportTempService.selectBaseExportTempList(baseExportTemp);
        return getDataTable(list);
    }

    /**
     * 导出ureport2模板管理列表
     */
    @ApiOperation(value = "导出ureport2模板管理列表", notes = "导出ureport2模板管理列表")
    @ApiOperationSupport(order = 20)
    @PreAuthorize("@ss.hasPermi('base:baseExportTemp:export')")
    @Log(title = "ureport2模板管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseExportTemp baseExportTemp) {
        List<BaseExportTemp> list = baseExportTempService.selectBaseExportTempList(baseExportTemp);
        ExcelUtil<BaseExportTemp> util = new ExcelUtil<BaseExportTemp>(BaseExportTemp.class);
        util.exportExcel(list, "ureport2模板管理数据");
    }

    /**
     * 获取ureport2模板管理详细信息
     */
    @ApiOperation(value = "获取ureport2模板管理详细信息", notes = "获取ureport2模板管理详细信息")
    @ApiOperationSupport(order = 30)
    @PreAuthorize("@ss.hasPermi('base:baseExportTemp:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(baseExportTempService.selectBaseExportTempById(id));
    }

    /**
     * 新增ureport2模板管理
     */
    @ApiOperation(value = "新增ureport2模板管理", notes = "新增ureport2模板管理")
    @ApiOperationSupport(order = 40, ignoreParameters = {"baseExportTemp.id"})
    @PreAuthorize("@ss.hasPermi('base:baseExportTemp:add')")
    @Log(title = "ureport2模板管理", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestBody @Valid @ApiParam(required = true) BaseExportTemp baseExportTemp) {
        int i = baseExportTempService.insertBaseExportTemp(baseExportTemp);
        if (i > 0) {
            //返回主键
            return AjaxResult.success(baseExportTemp.getId());
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 修改ureport2模板管理
     */
    @ApiOperation(value = "修改ureport2模板管理", notes = "修改ureport2模板管理")
    @ApiOperationSupport(order = 50)
    @PreAuthorize("@ss.hasPermi('base:baseExportTemp:edit')")
    @Log(title = "ureport2模板管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@RequestBody @Valid @ApiParam(required = true) BaseExportTemp baseExportTemp) {
        int i = baseExportTempService.updateBaseExportTemp(baseExportTemp);
        if (i > 0) {
            //返回主键
            return AjaxResult.success(baseExportTemp.getId());
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 删除ureport2模板管理
     */
    @ApiOperation(value = "删除ureport2模板管理", notes = "删除ureport2模板管理")
    @ApiOperationSupport(order = 60)
    @PreAuthorize("@ss.hasPermi('base:baseExportTemp:remove')")
    @Log(title = "ureport2模板管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        //逻辑删除
        return toAjax(baseExportTempService.setDeleteByIds(ids));
    }

    /**
     * 查询ureport2模板管理列表---不进行分页
     */
    @ApiOperation(value = "查询ureport2模板管理列表---不进行分页", notes = "查询ureport2模板管理列表---不进行分页")
    @ApiOperationSupport(order = 70)
    @PreAuthorize("@ss.hasPermi('base:baseExportTemp:list')")
    @PostMapping("/getDataList")
    public AjaxResult getDataList(@RequestBody BaseExportTemp baseExportTemp) {
        List<BaseExportTemp> list = baseExportTempService.selectBaseExportTempList(baseExportTemp);
        return AjaxResult.success(list);
    }

    /**
     * 跳转到添加模板页面
     *
     * @return
     */
    @ApiOperation(value = "返回添加模板页面路径", notes = "返回添加模板页面路径")
    @ApiOperationSupport(order = 80)
    @GetMapping("/addTemp")
    @PreAuthorize("@ss.hasPermi('baseExportTemp:baseExportTemp:addTemp')")
    public String addTemp() {
        return configService.selectConfigByKey("ureport_url") + "/ureport/designer";
    }

    /**
     * 跳转到修改模板页面
     *
     * @return
     */
    @ApiOperation(value = "返回修改模板页面路径", notes = "返回修改模板页面路径")
    @ApiOperationSupport(order = 90)
    @GetMapping("/editTemp/{id}")
    @PreAuthorize("@ss.hasPermi('baseExportTemp:baseExportTemp:edit')")
    public String editTemp(@PathVariable Long id) {
        BaseExportTemp baseExportTemp1 = baseExportTempService.selectBaseExportTempById(id);
        if (baseExportTemp1 == null) {
            throw new SecurityException("当前模板不存在");
        }
        return configService.selectConfigByKey("ureport_url") + "/ureport/designer" + "?_u=" + baseExportTemp1.getPrefix() + baseExportTemp1.getTempFileName();
    }

    /**
     * 跳转到预览模板页面
     *
     * @return
     */
    @ApiOperation(value = "返回预览模板页面路径", notes = "返回预览模板页面路径")
    @ApiOperationSupport(order = 90)
    @GetMapping("/previwTemp/{id}")
    @PreAuthorize("@ss.hasPermi('baseExportTemp:baseExportTemp:previwTemp')")
    public String previwTemp(@PathVariable Long id) {
        BaseExportTemp baseExportTemp1 = baseExportTempService.selectBaseExportTempById(id);
        if (baseExportTemp1 == null) {
            throw new SecurityException("当前模板不存在");
        }
        String url = configService.selectConfigByKey("ureport_url") + "/ureport/preview";
        Map<String, String> params = new HashMap();
        params.put("_u", baseExportTemp1.getPrefix() + baseExportTemp1.getTempFileName());//文件地址,文件名
        params.put("_t", baseExportTemp1.getSpareParam1());//工具栏
        return CommonUtils.asUrlParams(url, params);
    }

    /**
     * 实际业务--跳转到预览模板页面
     *
     * @return
     */
    @ApiOperation(value = "实际业务--跳转到预览模板页面", notes = "实际业务--跳转到预览模板页面")
    @ApiOperationSupport(order = 90)
    @GetMapping("/businessPreviwTemp")
    public String businessPreviwTemp(@Valid BaseTempExportDTO baseTempExportDTO) {
        BaseExportTemp baseExportTemp1 = baseExportTempService.selectParamByCode(baseTempExportDTO.getCode());
        if (baseExportTemp1 == null) {
            throw new SecurityException("当前模板不存在");
        }
        String extraStr = "";
        if (StringUtils.isNotBlank(baseTempExportDTO.getOrderNo())) {
            extraStr = extraStr + "&orderNo=" + baseTempExportDTO.getOrderNo();
        }
        if (baseTempExportDTO.getExtraParams() != null) {
            String extraParams = baseTempExportDTO.getExtraParams().stream().collect(Collectors.joining(","));
            extraStr = extraStr + "&extraParams=" + extraParams;
        }
//        return "http://"+ IpUtils.getHostIp() +":"
//                +environment.getProperty("server.port")+"/ureport/preview?_u="
//                +baseExportTemp1.getPrefix()+baseExportTemp1.getTempFileName()
//                +"&orderNo="+orderNo;
        //直接跳转到PDF预览页面
//        return "http://"+ IpUtils.getHostIp() +":"
//                +environment.getProperty("server.port")+"/ureport/pdf/show?_u="
//                +baseExportTemp1.getPrefix()+baseExportTemp1.getTempFileName()
//                +"&orderNo="+orderNo;
        return configService.selectConfigByKey("ureport_url") + "/ureport/pdf/show?_u="
                + baseExportTemp1.getPrefix() + baseExportTemp1.getTempFileName()
                + extraStr;
    }

    /**
     * 实际业务--导出excel
     *
     * @return
     */
    @ApiOperation(value = "实际业务--导出excel", notes = "实际业务--导出excel")
    @ApiOperationSupport(order = 100)
    @GetMapping("/businessExportExcel")
    public String businessExportExcel(@Valid BaseTempExportDTO baseTempExportDTO) {
        BaseExportTemp baseExportTemp1 = baseExportTempService.selectParamByCode(baseTempExportDTO.getCode());
        if (baseExportTemp1 == null) {
            throw new SecurityException("当前模板不存在");
        }
//        return "http://"+ IpUtils.getHostIp() +":"
//                +environment.getProperty("server.port")+"/ureport/preview?_u="
//                +baseExportTemp1.getPrefix()+baseExportTemp1.getTempFileName()
//                +"&orderNo="+orderNo;previwTemp
        //直接跳转到PDF预览页面
//        return "http://"+ IpUtils.getHostIp() +":"
//                +environment.getProperty("server.port")+"/ureport/excel?_u="
//                +baseExportTemp1.getPrefix()+baseExportTemp1.getTempFileName()
//                +"&orderNo="+orderNo;
        String extraStr = "";
        if (StringUtils.isNotBlank(baseTempExportDTO.getOrderNo())) {
            extraStr = extraStr + "&orderNo=" + baseTempExportDTO.getOrderNo();
        }
        if (baseTempExportDTO.getBeginTime() != null) {
            extraStr = extraStr + "&beginTime=" + DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", baseTempExportDTO.getBeginTime());
        }
        if (baseTempExportDTO.getEndTime() != null) {
            extraStr = extraStr + "&endTime=" + DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", baseTempExportDTO.getEndTime());
        }
        if (baseTempExportDTO.getExtraParams() != null) {
            String extraParams = baseTempExportDTO.getExtraParams().stream().collect(Collectors.joining(","));
            extraStr = extraStr + "&extraParams=" + extraParams;
        }
        return configService.selectConfigByKey("ureport_url") + "/ureport/excel?_u="
                + baseExportTemp1.getPrefix() + baseExportTemp1.getTempFileName()
                + extraStr;
    }
}
